﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using IMCOM.Imagiality.Core;
namespace IMCOM.Imagiality.ImageProcessing.Canvas2D
{
    public class Canvas2DGUI : Panel, CanvasImageWrapperObserver
    {
        private Canvas2DImageEventListener imageEventListener;
        private Canvas2DButtonEventListener buttonEventListener;


        private PictureBox pictureBoxMain;
        private CanvasImageWrapper wrapper;
        private ToolStripContainer toolStripContainerMain;
        private Label labelHint;

        //button toolbar
        private ToolStrip toolStripMain;
        private ToolStripButton toolStripButtonZoomIn;
        private ToolStripButton toolStripButtonZoomOut;
        private ToolStripButton toolStripButtonZoomOriginal;
        private ToolStripButton toolStripButtonZoomFit;


        public CanvasImageWrapper ImageWrapper
        {
            set 
            {
                this.wrapper = value;
                wrapper.AddObserver(this);
                bindButtonEventListener();
                Notify();
            }
        }

        public Canvas2DImageEventListener ImageEventListener
        {
            set 
            {
                imageEventListener = value;
                pictureBoxMain.MouseDown += new MouseEventHandler(imageEventListener.ImageMouseDown);
                pictureBoxMain.MouseMove += new MouseEventHandler(imageEventListener.ImageMouseMoved);
                pictureBoxMain.MouseUp += new MouseEventHandler(imageEventListener.ImageMouseUp);
                pictureBoxMain.Paint += new PaintEventHandler(imageEventListener.ImagePainted);
                wrapper.AddObserver(imageEventListener);
            }
            get 
            {
                return imageEventListener;
            }
        }


        private void bindButtonEventListener()
        {
            buttonEventListener = new Canvas2DButtonEventListener(this, wrapper);
            toolStripButtonZoomIn.Click += new EventHandler(buttonEventListener.ZoomInClicked);
            toolStripButtonZoomOut.Click += new EventHandler(buttonEventListener.ZoomOutClicked);
            toolStripButtonZoomOriginal.Click += new EventHandler(buttonEventListener.ZoomOriginalClicked);
            toolStripButtonZoomFit.Click += new EventHandler(buttonEventListener.ZoomFitClicked);
        }


        public Canvas2DGUI(CanvasImageWrapper imageWrapper) : this()
        {
            ImageWrapper = imageWrapper;
        }

        public Canvas2DGUI()
        {

   
            Dock = DockStyle.Fill;

            toolStripContainerMain = new ToolStripContainer();
            Controls.Add(toolStripContainerMain);
            toolStripContainerMain.Dock = DockStyle.Fill;

            Panel panelPictureBoxMain = new Panel();
            toolStripContainerMain.ContentPanel.Controls.Add(panelPictureBoxMain);
            panelPictureBoxMain.Dock = DockStyle.Fill;
            panelPictureBoxMain.AutoScroll = true;

            labelHint = new Label();
            toolStripContainerMain.ContentPanel.Controls.Add(labelHint);
            labelHint.TextAlign = ContentAlignment.MiddleCenter;

            labelHint.Left = (getContentPanelSize().Width - labelHint.Width) / 2;
            labelHint.Top = (getContentPanelSize().Height - labelHint.Height) / 2;

            labelHint.Anchor = AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom | AnchorStyles.Left;
            labelHint.BackColor = Color.FromArgb(100, 0, 0, 0);
            labelHint.MouseClick += new MouseEventHandler(LabelHintClicked);

            pictureBoxMain = new PictureBox();
            pictureBoxMain.Parent = panelPictureBoxMain;
            pictureBoxMain.SizeMode = PictureBoxSizeMode.StretchImage;

            SizeChanged += new EventHandler(Canvas2DGUISizeChanged);

            // build toolbar
            toolStripMain = new ToolStrip();
            toolStripContainerMain.TopToolStripPanel.Controls.Add(toolStripMain);

            toolStripButtonZoomIn = new ToolStripButton(IMCOM.Imagiality.ImageProcessing.Properties.Resources.icon_zoom_in);
            toolStripButtonZoomIn.ToolTipText = "Click to zoom in";
            toolStripMain.Items.Add(toolStripButtonZoomIn);

            toolStripButtonZoomOut = new ToolStripButton(IMCOM.Imagiality.ImageProcessing.Properties.Resources.icon_zoom_out);
            toolStripButtonZoomOut.ToolTipText = "Click to zoom out";
            toolStripMain.Items.Add(toolStripButtonZoomOut);

            toolStripButtonZoomOriginal = new ToolStripButton(IMCOM.Imagiality.ImageProcessing.Properties.Resources.icon_zoom_original);
            toolStripButtonZoomOriginal.ToolTipText = "Click to view in original size";
            toolStripMain.Items.Add(toolStripButtonZoomOriginal);

            toolStripButtonZoomFit = new ToolStripButton(IMCOM.Imagiality.ImageProcessing.Properties.Resources.icon_zoom_fit);
            toolStripButtonZoomFit.ToolTipText = "Click to fit the picture on window";
            toolStripMain.Items.Add(toolStripButtonZoomFit);

        }

        public void ShowHint(String hint)
        {
            labelHint.Text = hint;
            labelHint.Show();

            /*Thread hintClosingThread = new Thread(HideHint);
            hintClosingThread.Start();*/
        }

        public void HideHint()
        {
            HideHint(5000);
        }

        public void HideHint(int milliSeconds)
        {
            Thread.Sleep(milliSeconds);
            labelHint.Hide();
        }

        public void Notify()
        {
            if (wrapper.ActiveImageIndex >= 0)
            {
                GenericImage genericImage = wrapper.ViewImage;
                Bitmap bitmap = genericImage.getBitmap();
                pictureBoxMain.Image = bitmap;
                pictureBoxMain.Width = bitmap.Width;
                pictureBoxMain.Height = bitmap.Height;
                CenterPictureBox();
            }
        }

        public void CenterPictureBox()
        {
            /*if (pictureBoxMain.Width < getContentPanelSize().Width)
                pictureBoxMain.Left = (getContentPanelSize().Width - pictureBoxMain.Width) / 2;
            else
                pictureBoxMain.Left = 0;

            if (pictureBoxMain.Height < getContentPanelSize().Height)
                pictureBoxMain.Top = (getContentPanelSize().Height - pictureBoxMain.Height) / 2;
            else
                pictureBoxMain.Top = 0;*/
        }

        public Size getContentPanelSize()
        {
            return toolStripContainerMain.ContentPanel.Size;
        }
      
        private void Canvas2DGUISizeChanged(object sender, EventArgs e)
        {
            CenterPictureBox();

            Refresh();
        }

        private void LabelHintClicked(object sender, MouseEventArgs e)
        {
            labelHint.Hide();
        }
    }
}
